function getCookie(c_name) {
    var c_value = document.cookie;
    console.log("Cookie: " + c_value);
    var c_start = c_value.indexOf(" " + c_name + "=");
    if (c_start == -1) {
        c_start = c_value.indexOf(c_name + "=");
    }
    if (c_start == -1) {
        c_value = null;
    } else {
        c_start = c_value.indexOf("=", c_start) + 1;
        var c_end = c_value.indexOf(";", c_start);
        if (c_end == -1) {
            c_end = c_value.length;
        }
        c_value = unescape(c_value.substring(c_start, c_end));
    }
    return c_value;
};
function convertPositionHeight(height, i, x) {
    if (i > 6) {
        height = height - x;
    }
};
function getSizeCCNode(Node) {

    return Node.getTextureRect();
};
function actionMoveCard(object, i) {
    var size = cc.Director.getInstance().getWinSize();
    var MoveBy0 = cc.MoveTo.create(0.3, cc.p(size.width / 2 - size.width * 10 / 960, size.height / 2 + 70));
    var MoveBy1 = cc.MoveTo.create(0.3, cc.p(size.width / 2 + size.width * 50 / 960, size.height / 2 + 70));
    var MoveBy2 = cc.MoveTo.create(0.3, cc.p(size.width / 2 + size.width * 110 / 960, size.height / 2 + 70));
    var MoveBy3 = cc.MoveTo.create(0.3, cc.p(size.width / 2 + size.width * 170 / 960, size.height / 2 + 70));
    var MoveBy4 = cc.MoveTo.create(0.3, cc.p(size.width / 2 + size.width * 230 / 960, size.height / 2 + 70));
    var MoveBy5 = cc.MoveTo.create(0.3, cc.p(size.width / 2 + size.width * 290 / 960, size.height / 2 + 70));
    switch (i) {
        case 0:
            object.runAction(MoveBy0);
            break;
        case 1:
            object.runAction(MoveBy1);
            break;
        case 2:
            object.runAction(MoveBy2);
            break;
        case 3:
            object.runAction(MoveBy3);
            break;
        case 4:
            object.runAction(MoveBy4);
            break;
        case 5:
            object.runAction(MoveBy4);
            break;
    }
////    c: 0, // Clubs
//      d: 1, // Diamonds
//        h: 2, // Hearts
//        s: 3, // Spades
//        j: 4  // Rank//
//
};
function convertColorCard(s) {
    switch (s) {
        case 0:
            return 's';
            break;
        case 1:
            return 'c';
            break;
        case 2:
            return 'd';
            break;
        case 3:
            return 'h';
            break;
        case 4:
            return 'j';
            break;
        default :
            return s;
    }
}

//------------ Sort by index ---------------//

function sortByIndex(arr1) {

    for (var i = 0; i < arr1.length - 1; i++) {
        var min = i;
        for (var j = i + 1; j < arr1.length; j++) {
            if (arr1[j].index < arr1[min].index) {
                min = j;
            }
        }
        if (min != i) {
            var c = arr1[min];
            arr1[min] = arr1[i];
            arr1[i] = c;
        }
    }
    return arr1;
}
//------------ Check Existence of selected cards --------------------//

function checkArrSimilarity(arr1, arr2) {
    var x = 0;
    if (arr1.length == arr2.length) {
        for (var i = 0; i < arr1.length; i++) {
            if (arr1[i].index == arr2[i].index) {
                x++;
            }
        }

    }
    else {
        x = 0;
    }
    return x;
}

function checkArrExistence(arr1, arr2) {
    var ret = null;
    for (var i = 0; i < arr2.length; i++) {
        var nsame = 0;
        if (arr2[i].length != arr1.length) {
            continue;
        }
        for (var j = 0; j < arr1.length; j++) {
            for (var k = 0; k < arr2[i].length; k++) {
                if (arr1[j].index == arr2[i][k].index) {
                    nsame++;
                    break;
                }
            }
        }
        if (nsame == arr1.length) {
            ret = i;
            break;
        }
    }
    return ret;
}

//-----------------------------------------------------------------//


function checkArr(arrInput1, arrInput2) {
    var check = 0;
    sortByIndex(arrInput2);
    for (var i = 0; i < arrInput1.length; i++) {
        for (var j = 0; j < arrInput2.length; j++) {
            if (arrInput1[i] == arrInput2[j].index) {
                check++;
            }
        }
    }
    return check;
}
function generateArr(arr1, arr2) {
    var mArrayCard = [];
    var length = arr1.length;
    for (var i = 0; i < arr2.length; i++) {
        if (checkArr(arr1, arr2[i]) == length) {
            mArrayCard.push(arr2[i]);
        }
    }
    return mArrayCard;
}

function convertRankCard(rank) {
    switch (rank) {
        case 14 :
            return 'a';
            break;
        case 11 :
            return 'j';
            break;
        case 12 :
            return 'q';
            break;
        case 13 :
            return 'k';
            break;
        case 16:
            return 'joker';
            break;
        case 15:
            return '2';
            break;
        default :
            return rank;

    }

};
function setCookie(c_name, value, exdate) {
    var c_value = escape(value)
        + ((exdate == null) ? "" : "; expires=" + exdate.toUTCString());
    console.log("Value: " + c_value);
    document.cookie = c_name + "=" + c_value;
};

function convertToRadixString(score, radix) {
    var str_score = score + "";
    while (str_score.length < radix) {
        str_score = "0" + str_score;
    }
    return str_score;
}

function convertSecondToMMSS(seconds) {
    var minute = Math.floor(seconds / 60);
    var second = seconds % 60;

    if (minute < 10) {
        minute = '0' + minute;
    }
    if (second < 10) {
        second = '0' + second;
    }
    return minute + ":" + second;
}

function convertDate2Label(sDate) {
    // 2014-09-11 --> 09/11
    return sDate[5] + sDate[6] + "/" + sDate[8] + sDate[9];
}

function convertDate2HHMMSS(sDate) {
    var time = new Date(sDate);
    var h = (time.getHours() < 10) ? ('0' + time.getHours()) : time.getHours();
    var m = (time.getMinutes() < 10) ? ('0' + time.getMinutes()) : time.getMinutes();
    var s = (time.getSeconds() < 10) ? ('0' + time.getSeconds()) : time.getSeconds();

    return h + ":" + m + ":" + s;
}

function genRandomInt(min, max) {
    // Get random following suggestion from Mozillafirefox;
    return Math.floor((Math.random() * (max - min + 1)) + min);
}

function shuffle(array) {
    var counter = array.length, temp, index;

    // While there are elements in the array
    for (var i = counter - 1; i >= 1; i--) {
        // Pick a random index
        index = this.genRandomInt(0, i);

        // And swap the last element with it
        temp = array[i];
        array[i] = array[index];
        array[index] = temp;
    }

    return array;
}
/**
 * Play sound
 * @param sound
 * @param options
 */
function playSound(sound, options) {
    console.log('playsound: ' + sound);
    try {
        var soundInstance;
        if (options) {
            soundInstance = createjs.Sound.play(sound, options);
        }
        else {
            soundInstance = createjs.Sound.play(sound);
        }
        return soundInstance;
    }
    catch (err) {
        alert(err);
        return null;
    }
}

/**
 * Stop play sound
 */
function stopSound() {
    try {
        createjs.Sound.stop();
    }
    catch (err) {
        alert(err);
    }
}

/*
*
*  Check index card
*
* */

function CheckIndex(mCard, mTurnCard){
    for(var i=0; i< mTurnCard.length; i++){
        if(mCard.index == mTurnCard[i]){
            return true;
        }
    }
    return false;
}